import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import base from '@src/store/base';
import {yuan} from "@src/common/utils";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor']
  }],
  properties: {
    pointsObj: {
      type: Object
    }
  },
  options: {
    addGlobalClass: true,
  },
  observers: {
    inputVal() {
      if (this.data.inputVal && this.data.inputVal > 0) {
        let deduction = ((this.data.inputVal / this.data.pointsObj.perUse) * (this.data.pointsObj.perReduce / 100)).toFixed(2)
        this.setData({
          deduction: deduction
        })
      }

    }
  },

  data: {
    dialogShow: false,
    inputVal: 0,
    hiddenTips: true,
    deduction: null,
  },
  methods: {
    closeDialog() {
      this.setData({dialogShow: false})
    },
    open() {
      this.setData({dialogShow: true})
    },
    showTips() {
      this.setData({hiddenTips: false})
    },
    noShowTips() {
      console.info("noShowTips")
      this.setData({hiddenTips: true})
    },
    subtractFunc() {
      let inputVal = this.data.inputVal;
      let deduction;
      if (!(inputVal <= 0)) {
        inputVal = --inputVal
        // deduction = ((inputVal / this.data.pointsObj.perUse) * (this.data.pointsObj.perReduce / 100)).toFixed(2)
      }

      this.setData({
        inputVal: inputVal,
        // deduction: deduction
      })
    },

    //判断是否是单位积分的倍数
    checkoutMultiple(num) {
      if (num % this.data.pointsObj.perUse) {
        console.log("请输入" + this.data.pointsObj.perUse + "得倍数");
        wx.showToast({
          title: "请输入" + this.data.pointsObj.perUse + "的倍数",
          icon: 'none'
        })
        // this.setData({
        //   inputVal: this.data.pointsObj.perUse
        // })
        return false;
      } else {
        return true;
      }

    },
    //加
    sumFunc() {
      let inputVal = this.data.inputVal
      if (!(inputVal >= this.data.pointsObj.maxPointsNum)) {
        inputVal = ++inputVal;
        console.log('inputVal', inputVal)
        this.setData({
          inputVal: inputVal
        })
      } else {
        this.setData({
          inputVal: this.data.pointsObj.maxPointsNum
        })
      }

      let deduction = ((this.data.inputVal / this.data.pointsObj.perUse) * (this.data.pointsObj.perReduce / 100)).toFixed(2)
      this.setData({
        deduction: deduction
      })
    },
    //INPUT输入框失去焦点
    inputBindblur(val) {
      let num = parseInt(val.detail.value);
      let flag = this.checkoutMultiple(num);
      if (flag) {
        if (num > this.data.pointsObj.maxPointsNum) {
          this.setData({
            inputVal: this.data.pointsObj.maxPointsNum
          })
        } else {
          this.setData({
            inputVal: num
          })
        }
      } else {
        // this.setData({
        //   inputVal: this.data.pointsObj.perUse
        // })
      }

      this.setData({
        inputVal: num
      })


      // let deduction = ((this.data.inputVal / this.data.pointsObj.perUse) * (this.data.pointsObj.perReduce / 100)).toFixed(2)
      // this.setData({
      //   deduction: deduction
      // })
      console.log('val', val)
    },
    //使用积分
    userPerferential() {

      if ((this.data.inputVal && (this.data.inputVal >= this.data.pointsObj.perUse && this.data.inputVal <= this.data.pointsObj.maxPointsNum))) {
        let flag = this.checkoutMultiple(this.data.inputVal);

        console.log('flag', flag)

        if (flag) {
          this.closeDialog();
          let val = {
            num: this.data.inputVal,
            discountName: "积分抵扣",
            discountFee: this.data.deduction * 100,
            discountDesc: this.data.inputVal + "积分抵扣" + this.data.deduction + "元",
          }
          this.triggerEvent("points", val)
        }
      } else {
        wx.showToast({
          title: '请输入正确的积分',
          icon: 'none'
        })
      }
    },
    //不使用积分
    unUserPerferential() {
      this.setData({
        inputVal: 0,
      })
      this.closeDialog();
      this.triggerEvent("points", null)
    }
  }
})
